class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    bool used[10] = {false};

    void dfs(vector<int>& nums, int pos)
    {
        ret.push_back(path);
        for (int i = pos; i < nums.size(); i++)
        {
            if (i != 0 && nums[i] == nums[i - 1] && !used[i - 1])
                continue;
            
            used[i] = true;
            path.push_back(nums[i]);
            dfs(nums, i + 1);
            path.pop_back();
            used[i] = false;
        }
    }
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums, 0);
        return ret;
    }
};